// Perform verification and add item to list of item in addshipment form.
function addItem() {
	var valid = true;
    var itemForm = document.getElementById('addShipmentForm');
    var display = document.getElementById('dispMess');

    // Display error if the default color item (default is "pick a color"
    // is selected
    if (itemForm.itemColor.options[0].selected) {
        document.getElementById('mItemColor').className = 'err';
        valid = false;
    } else {
        document.getElementById('mItemColor').className = 'hiddenMessage';
    }

    // Display error if the name of the item being added is empty.
    // after string is being trimmed for spaces.
    itemForm.itemName.value = jQuery.trim(itemForm.itemName.value);

    if (valid) {
        display.className = 'pass';
        display.innerHTML = "Item Added Successfully.";
		for (i = 0; i < itemForm.itemQuantity.value; i++)
		{
 
 			if (itemForm.itemList.value === "") {
            itemForm.itemList.value += "[[\"" + itemForm.itemName.value
                    + "\", \"" + itemForm.itemColor.value + "\"]]";
        } else {
            var newString = itemForm.itemList.value.substring(0,
                    itemForm.itemList.value.length - 1);
            newString += ",[\"" + itemForm.itemName.value + "\", \""
                    + itemForm.itemColor.value + "\"]]";
            itemForm.itemList.value = newString;
        }
			$('#specTable').dataTable().fnAddData(
                [ itemForm.itemName.value, itemForm.itemColor.value]);
		}
		  // Set display messages back to invisiable.
		  
        $('#specTable').removeClass('hiddenTable');
        
        // Reset field values.
        itemForm.itemName.value = "";
        itemForm.itemColor.options[0].selected = true;
        display.style.display = 'block';

    } else {
        display.className = 'err';
        display.innerHTML = "Please Check your Input.";
    }

}

// Add shipment entry to database and datatable for clientView.
function addNewShipment() {

    // Get the data from all the fields
    var barcode = $('input[name=barcode]');
    var dest = $('select[name=dest] option:selected');
    var itemList = $('textarea[name=itemList]');
    var requirement = $('textarea[name=requirement]');
    var description = $('textarea[name=description]');
	if (
    // Display error message if barcode is empty after
	ValidateInputBox('barcode', 'mBarcode') &&
 
    // Display error message if the default destination value is
    // selected.
	ValidateDropBox('dest', 'mDest') &&
   
    // Display error message if itemList is empty. This is not visiable
	ValidateTextareaBox('itemList', 'mItemList'))
    {
		//Generate list of data from item table.
		var tableData = $('#specTable').dataTable().fnGetData();
		var listOfSpec = ''; //JSON list
		var tableListOfSpec = ""; //List used to insert into table.
		
		for (i = 0; i < tableData.length; i++) {
			if (listOfSpec === '') {
				listOfSpec += "[[\"" + tableData[i][0]
						+ "\", \"" + tableData[i][1] + "\"]]";
			}
			else {
				var newString = listOfSpec.substring(0,
						listOfSpec.length - 1);
				newString += ",[\"" + tableData[i][0] + "\", \""
						+ tableData[i][1] + "\"]]";
				listOfSpec = newString;
        	}
			if (i === tableData.length - 1) {	
			tableListOfSpec += '<span>' + tableData[i][0] + ' -- ' + tableData[i][1] + '</span>'; 	
			} else {
				tableListOfSpec += '<span>' + tableData[i][0] + " -- " + tableData[i][1] + '</span><br />'; 	
			}
		}
		
        // Orgainize data.
        var data = 'barcode=' + barcode.val() + '&dest=' + dest.val()
                + '&itemList=' + listOfSpec + '&requirement='
                + requirement.val() + '&description=' + description.val();

        // start the ajax
        $
                .ajax({
                    url : "/add/shipment",
                    type : "POST",
                    data : data, // pass the data
                    cache : false,

                    success : function() {

                        // Display Success message.
                        document.getElementById('mAddMessage').innerHTML = "Add Shipment Successfully.";
                        document.getElementById('mAddMessage').className = 'pass';

                        // hide the form
                        $('#addShipment').fadeOut('slow');
                        document.getElementById('addShipLink').innerHTML = '<img src="img/details_open.png" alt"Add Shipment"/>Click Here to Add New Shipment.';
                      

                       
						// Clear Destination Point Table
						$('#specTable').dataTable().fnClearTable();

                        // Add entry to datatable
                        $('#clientTable')
                                .dataTable()
                                .fnAddData(
                                        [
                                                barcode.val(),
                                                tableListOfSpec,
                                                "-1",
                                                "Processing",
                                                description.val(),
                                                requirement.val() ]);
												
						 // Reset add shipment form.
                        document.getElementById('addShipmentForm').reset();
                    },

                    failure : function() {
                    },
                    error : function() {
                        alert('Sorry, unexpected error. Please try again later.');
                    }
                });
    } else {
        document.getElementById('mAddMessage').innerHTML = "Please verify your Inputs.";
        document.getElementById('mAddMessage').className = 'err';
    }

}